﻿using System;
using System.Web.UI.WebControls;
using WorkOrder.Business;
using WorkOrder.Model;
using System.Drawing;

namespace WorkOrder
{
    public partial class addWorkOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Model_Table_Work_order model;
            Business_Table_Work_order b_t_work_order = new Business_Table_Work_order();
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "windohref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.parent.location.href='../login.aspx';</script>");
            }

            if (Page.IsPostBack)
            {
                if (Request.Params["a"] == "1" && Request.Params["id"] != null)
                {
                    #region 修改信息
                    if (!new Business_Table_Permission().HasModify(Session["UserID"].ToString(), "Table_Work_order"))
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = "提示信息：您没有修改工单信息的权限！";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您没有修改工单信息的权限！');</script>");
                        return;
                    }

                    if (hidTown.Value == "") hidTown.Value = "NULL";
                    Model_Table_Area temp = b_t_work_order.GetAreaByTown(hidTown.Value);
                    if (string.IsNullOrEmpty(seleArea.Value) && temp != null) seleArea.Value = temp.Area_id;
                    model = new Model_Table_Work_order();
                    model.Order_id = Request.Params["id"];
                    model.Order_number = Server.HtmlEncode(txtOrderNo.Value.Trim());
                    model.GroupCode = Server.HtmlEncode(txtGroupcode.Text.Trim());
                    model.IsBack = rblIsBackTrue.Checked;
                    model.Product_id = !string.IsNullOrEmpty(ddlProduct.SelectedValue) ? ddlProduct.SelectedValue : "NULL";
                    model.Order_type_id = !string.IsNullOrEmpty(seleOrderType.Value) ? seleOrderType.Value : "NULL";
                    model.Town_id = hidTown.Value;
                    model.Title = Server.HtmlEncode(txtTitle.Value.Trim());
                    model.OrderContent = Server.HtmlEncode(txtOrderContent.Text.Trim());
                    model.EntryPerson = Session["UserID"].ToString();
                    model.Remark = Server.HtmlEncode(txtRemark.Text.Trim());
                    model.Area_id = !string.IsNullOrEmpty(seleArea.Value) ? seleArea.Value : "NULL";
                    model.AccessMode = seleAccess.Value;
                    model.CustomerCode = txtCustomerCode.Text.Trim();
                    if (txtClustering_client.Text.Trim() != "")
                        model.ClusteringClient = txtClustering_client.Text.Trim();
                    else
                        model.ClusteringClient = "";

                    model.BackReasonComment = Server.HtmlEncode(txtReasonComment.Text.Trim());
                    if (ddlReasonType.SelectedValue == "0") model.BackReason = ""; else model.BackReason = ddlReasonType.SelectedValue;
                   
                    try { if (txtPtime.Text.Trim() != "") model.TaskTime = Convert.ToDateTime(txtPtime.Text.Trim()).ToString(); }
                    catch
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = "提示信息：时间格式错误，请重新输入！";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "wip3nft", "<script type='text/javascript'>alert('提示信息：时间格式错误，请重新输入！');window.close();</script>");
                        return;
                    }
                    try { if (txtAnalysis_time.Text.Trim() != "") model.Analysis_time = Convert.ToDateTime(txtAnalysis_time.Text.Trim()).ToString(); }
                    catch
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = "提示信息：时间格式错误，请重新输入！";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "winft", "<script type='text/javascript'>alert('提示信息：时间格式错误，请重新输入！');window.close();</script>");
                        return;
                    }
                    try { if (txtConfirm_time.Text.Trim() != "") model.Confirm_time = Convert.ToDateTime(txtConfirm_time.Text.Trim()).ToString(); }
                    catch
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = "提示信息：时间格式错误，请重新输入！";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "wipnft", "<script type='text/javascript'>alert('提示信息：时间格式错误，请重新输入！');window.close();</script>");
                        return;
                    }
                    try { if (txtAudit_time.Text.Trim() != "") model.Audit_time = Convert.ToDateTime(txtAudit_time.Text.Trim()).ToString(); }
                    catch
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = "提示信息：时间格式错误，请重新输入！";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "wi8nft", "<script type='text/javascript'>alert('提示信息：时间格式错误，请重新输入！');window.close();</script>");
                        return;
                    }
                    try { if (txtConfig_time.Text.Trim() != "") model.Config_time = Convert.ToDateTime(txtConfig_time.Text.Trim()).ToString(); }
                    catch
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = "提示信息：时间格式错误，请重新输入！";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "winytft", "<script type='text/javascript'>alert('提示信息：时间格式错误，请重新输入！');window.close();</script>");
                        return;
                    }
                    try { if (txtFinish.Text.Trim() != "") model.FinishTime = Convert.ToDateTime(txtFinish.Text.Trim()).ToString(); }
                    catch
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = "提示信息：时间格式错误，请重新输入！";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "wi8nf", "<script type='text/javascript'>alert('提示信息：时间格式错误，请重新输入！');window.close();</script>");
                        return;
                    }
                    
                    if (rblIsBackFalse.Checked) { model.BackReason = ""; model.BackReasonComment = ""; }
                    
                    if (b_t_work_order.Update(model))
                    {
                        seleArea.Value = "";
                        seleOrderType.Value = "0";
                        txtOrderContent.Text = "";
                        txtOrderNo.Value = "";
                        txtTitle.Value = "";
                        hidTown.Value = "";
                        ddlProduct.SelectedIndex = ddlProduct.Items.Count - 1;
                        seleTown.SelectedIndex = seleTown.Items.Count - 1;
                        seleAccess.SelectedIndex = 0;
                        ddlReasonType.SelectedIndex = ddlReasonType.Items.Count - 1;
                        txtGroupcode.Text = "";
                        seleArea.Value = "0";
                        rblIsBackFalse.Checked = true;
                        txtRemark.Text = "";
                        txtFinish.Text = "";
                        txtAnalysis_time.Text = "";
                        txtReasonComment.Text = "";
                        txtAudit_time.Text = "";
                        txtConfig_time.Text = "";
                        txtPtime.Text = "";
                        txtConfirm_time.Text = "";
                        txtClustering_client.Text = "";
                        lblMsg.ForeColor = Color.DarkGreen;
                        txtCustomerCode.Text = "";
                        lblMsg.Text = "提示信息：成功保存一条记录！";
                    }                    
                    #endregion
                }
            }
            else if (Request.Params["a"] == "0")
            {
                Business_Table_Work_order b_t_order = new Business_Table_Work_order();
                ddlProduct.Items.Clear();
                ddlProduct.DataTextField = "product_name";
                ddlProduct.DataValueField = "product_id";
                ddlProduct.DataSource = b_t_order.GetProduct();
                ddlProduct.DataBind();
                ddlProduct.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlProduct.SelectedIndex = ddlProduct.Items.Count - 1;
                seleTown.Items.Clear();
                seleTown.DataTextField = "town_name";
                seleTown.DataValueField = "town_id";
                seleTown.DataSource = b_t_order.GetTown();
                seleTown.DataBind();
                seleTown.Items.Add(new ListItem("-- 请选择 --", "0"));
                seleTown.SelectedIndex = seleTown.Items.Count - 1;
                ddlReasonType.Items.Clear();
                ddlReasonType.DataValueField = "reason_type";
                ddlReasonType.DataTextField = "reason_type";
                ddlReasonType.DataSource = b_t_order.GetReasonType();
                ddlReasonType.DataBind();
                ddlReasonType.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlReasonType.SelectedIndex = ddlReasonType.Items.Count - 1;
                txtGroupcode.Text = "";
                seleArea.Value = "";
                seleOrderType.Value = "";
                txtOrderContent.Text = "";
                txtOrderNo.Value = "";
                txtTitle.Value = "";
                txtAnalysis_time.Text = "";
                txtReasonComment.Text = "";
                txtAudit_time.Text = "";
                txtConfig_time.Text = "";
                txtPtime.Text = "";
                txtConfirm_time.Text = "";
                txtClustering_client.Text = "";
                rblIsBackFalse.Checked = true;
            }
            else if (Request.Params["a"] == "1" && Request.Params["id"] != null)
            {
                Business_Table_Work_order b_t_order = new Business_Table_Work_order();
                model = b_t_order.GetData(Request.Params["id"]);
                ddlProduct.Items.Clear();
                ddlProduct.DataTextField = "product_name";
                ddlProduct.DataValueField = "product_id";
                ddlProduct.DataSource = b_t_order.GetProduct();
                ddlProduct.DataBind();
                ddlProduct.Items.Add(new ListItem("-- 请选择 --", "0"));
                if (string.IsNullOrEmpty(model.Product_id))
                    ddlProduct.SelectedIndex = ddlProduct.Items.Count - 1;
                else
                    ddlProduct.SelectedIndex = ddlProduct.Items.IndexOf(ddlProduct.Items.FindByValue(model.Product_id));
                seleTown.Items.Clear();
                seleTown.DataTextField = "town_name";
                seleTown.DataValueField = "town_id";
                seleTown.DataSource = b_t_order.GetTown();
                seleTown.DataBind();
                seleTown.Items.Add(new ListItem("-- 请选择 --", "0"));
                if (string.IsNullOrEmpty(model.Town_id))
                    seleTown.SelectedIndex = seleTown.Items.Count - 1;
                else
                    seleTown.SelectedIndex = seleTown.Items.IndexOf(seleTown.Items.FindByValue(model.Town_id));
                ddlReasonType.Items.Clear();
                ddlReasonType.DataValueField = "reason_type";
                ddlReasonType.DataTextField = "reason_type";
                ddlReasonType.DataSource = b_t_order.GetReasonType();
                ddlReasonType.DataBind();
                ddlReasonType.Items.Add(new ListItem("-- 请选择 --", "0"));
                if (!string.IsNullOrEmpty(model.BackReason))
                    ddlReasonType.SelectedIndex = ddlReasonType.Items.IndexOf(ddlReasonType.Items.FindByValue(model.BackReason));
                else
                    ddlReasonType.SelectedIndex = ddlReasonType.Items.Count - 1;
                txtOrderNo.Disabled = true;
                seleArea.Value = model.Area_id;
                seleOrderType.Value = model.Order_type_id;
                txtOrderContent.Text = model.OrderContent;
                txtOrderNo.Value = model.Order_number;
                txtReasonComment.Text = model.BackReasonComment;
                txtTitle.Value = model.Title;
                txtRemark.Text = model.Remark;
                txtGroupcode.Text = model.GroupCode;
                txtPtime.Text = model.TaskTime;
                hidTown.Value = model.Town_id;
                txtFinish.Text = model.FinishTime;
                txtAnalysis_time.Text = model.Analysis_time;
                txtAudit_time.Text = model.Audit_time;
                txtConfig_time.Text = model.Config_time;
                txtConfirm_time.Text = model.Confirm_time;
                seleAccess.Value = model.AccessMode;
                txtCustomerCode.Text = model.CustomerCode;
                txtClustering_client.Text = model.ClusteringClient;
                if (model.IsBack) rblIsBackTrue.Checked = true; else rblIsBackFalse.Checked = true;
                
            }
        }
    }
}
